//-------------------------------------------------------------------------------------
//propertydata

function propertydata()
	{
	var tempObj = xpathFirst('//td[@id=\''+SCRIPT.appID+'_content_row\']//div[contains(text(), \'Cash Flow\')]');
	tempObj2=tempObj.firstChild.nextSibling
	var income = tempObj.innerHTML.split('>')[1];
	income = parseInt(income.replace(/,/g, ''));
	if (tempObj && tempObj.innerHTML.match(/every (.+) minutes/))
		{
			var payments = 1440 / parseInt(RegExp.$1); //this codepath not tested
		}
		else
		{
	    		var payments = 24;
		}
	var crewPropertyRowPath = "//form[@id='app16421175101_properties_viral_purchase']";
	var crewPropertyRow = xpathFirst(crewPropertyRowPath);
	
	var allPropertyRowsPath = "//form[@id='app16421175101_properties_purchase']";
	var allPropertyRows = xpath(allPropertyRowsPath);

	var property2RowsPath = "//form[@id='app16421175101_properties_purchase2']";
	var property2Rows = xpath(property2RowsPath);

	var allProperties = new Array();
	
//crew
	var currentProperty = { id:0, roi:0, cost:0, name:'', row:0, path:'', income:0, mobsize:0, amount:0, owned:0, requiredId:0, requiredCost:0, requiredName:'' }
	var currentRowHtml = crewPropertyRow.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML;
	var str=currentRowHtml.substring(currentRowHtml.indexOf('Income: <strong class="money"><img src="http://facebook2.pirates.static.zynga.com/graphics/icon-blood.gif">')+107);
	str = str.substring(0,str.indexOf('<'));
	currentProperty.income=parseInt(str.replace(/,/g, ''));
	str = currentRowHtml.substring(currentRowHtml.indexOf('blood.gif">')+11);
	str = str.substring(str.indexOf('blood.gif">')+11);
	str = str.substring(0,str.indexOf('<'));
	currentProperty.cost=parseInt(str.replace(/,/g, ''));
	if (currentProperty.income > 0 && currentProperty.cost > 0)
		{
        	currentProperty.roi = currentProperty.income / (currentProperty.cost + currentProperty.requiredCost);
        	}
        if (currentProperty.roi > 0)
        	{
		var roiTime = (1/currentProperty.roi) / payments; // days
		if (roiTime > 3652.5)
			{ // display years
			roiTime /= 365.25;
			var roiTimeText = ' years)';
			}
		else if (roiTime > 365.25)
			{ // display months
			roiTime /= 30.4375;
			var roiTimeText = ' months)';
			}
		else
			{
        		var roiTimeText = ' days)';
        		}
        	}
	currentProperty.roi = Math.round(currentProperty.roi * 1000000)/1000000;
        var str = 'ROI: ' + currentProperty.roi + ' (' + (Math.round(roiTime * 100)/100) + roiTimeText;
	var roiText = document.createElement("div");
	roiText.innerHTML=str;
	crewPropertyRow.parentNode.parentNode.parentNode.insertBefore(roiText,allPropertyRows.snapshotItem(currentRow).parentNode.parentNode.parentNode.nextSibling);
	str = currentRowHtml.substring(currentRowHtml.indexOf('<strong>')+8);
	str = str.substring(0,str.indexOf('<'));
	currentProperty.name = str;
	str = currentRowHtml.substring(currentRowHtml.indexOf('Owned:')+6);
	str = str.substring(0,str.indexOf('<'));
	currentProperty.owned = parseInt(str);
	currentProperty.id = 0;
	str=currentRowHtml.substring(currentRowHtml.indexOf('Requires:')+68);
	currentProperty.mobsize = parseInt(str.substring(0,str.indexOf('<')));
	if(alliance>=currentProperty.mobsize && GM_getValue('buyCrewProperty', '')=='checked')
		{
		if (GM_getValue('buyCrewPropertyUnique', '')=='checked')
			{
			if (currentProperty.owned == 0) 
				{
				bestProperty = currentProperty;
				nextProperty = parseInt(currentProperty.cost);
				GM_setValue('nextProperty',nextProperty);
				}
			}
		else
			{
			bestProperty = currentProperty;
			nextProperty = parseInt(currentProperty.cost);
			GM_setValue('nextProperty',nextProperty);
			}
		}
	allProperties.push(currentProperty);

	for (var currentRow = 0; currentRow < allPropertyRows.snapshotLength; currentRow++)
		{
		var currentProperty = { id:0, roi:0, cost:0, name:'', row:0, path:'', income:0, mobsize:0, amount:0, owned:0, requiredId:0, requiredCost:0, requiredName:'' }

		var currentRowHtml = allPropertyRows.snapshotItem(currentRow).parentNode.parentNode.parentNode.innerHTML;
		var str=currentRowHtml.substring(currentRowHtml.indexOf('Income: <strong class="money"><img src="http://facebook2.pirates.static.zynga.com/graphics/icon-blood.gif">')+107);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.income=parseInt(str.replace(/,/g, ''));
		str = currentRowHtml.substring(currentRowHtml.indexOf('blood.gif">')+11);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.cost=parseInt(str.replace(/,/g, ''));
		if (currentProperty.income > 0 && currentProperty.cost > 0)
			{
	        	currentProperty.roi = currentProperty.income / (currentProperty.cost + currentProperty.requiredCost);
	        	}
	        if (currentProperty.roi > 0)
	        	{
			var roiTime = (1/currentProperty.roi) / payments; // days
			if (roiTime > 3652.5)
				{ // display years
				roiTime /= 365.25;
				var roiTimeText = ' years)';
				}
			else if (roiTime > 365.25)
				{ // display months
				roiTime /= 30.4375;
				var roiTimeText = ' months)';
				}
			else
				{
	        		var roiTimeText = ' days)';
	        		}
	        	}
		currentProperty.roi = Math.round(currentProperty.roi * 1000000)/1000000;
	        var str = 'ROI: ' + currentProperty.roi + ' (' + (Math.round(roiTime * 100)/100) + roiTimeText;
		var roiText = document.createElement("div");
		roiText.innerHTML=str;
		allPropertyRows.snapshotItem(currentRow).parentNode.parentNode.parentNode.insertBefore(roiText,allPropertyRows.snapshotItem(currentRow).parentNode.parentNode.parentNode.nextSibling);
		str = currentRowHtml.substring(currentRowHtml.indexOf('<strong>')+8);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.name = str;
		str = currentRowHtml.substring(currentRowHtml.indexOf('>Owned:')+8);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.owned = parseInt(str);
		currentProperty.id = currentRow + 1;
		if(bestProperty.roi < currentProperty.roi)
			{
			bestProperty = currentProperty;
			nextProperty = parseInt(currentProperty.cost);
			GM_setValue('nextProperty',nextProperty);
			}
		allProperties.push(currentProperty);
		}
	var length=allProperties.length;
	for (var currentRow = 0; currentRow < property2Rows.snapshotLength; currentRow++)
		{
		var currentProperty = { id:0, roi:0, cost:0, name:'', row:0, path:'', income:0, mobsize:0, amount:0, owned:0, requiredId:0, requiredCost:0, requiredName:'' }
		var currentRowHtml = property2Rows.snapshotItem(currentRow).parentNode.parentNode.parentNode.innerHTML; //income is at the very end
		var str=currentRowHtml.substring(currentRowHtml.indexOf('Income: <strong class="money"><img src="http://facebook2.pirates.static.zynga.com/graphics/icon-blood.gif">')+107);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.income=parseInt(str.replace(/,/g, ''));
		str = currentRowHtml.substring(currentRowHtml.indexOf('blood.gif">')+11);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.cost=parseInt(str.replace(/,/g, ''));
		str = currentRowHtml.substring(currentRowHtml.indexOf('Consumes: ')+10);
	        str = str.substring(0,str.indexOf('<'));
	        currentProperty.requiredName = str;
		for (var j=0; j < allProperties.length; j++)
			{
			if (allProperties[j].name == currentProperty.requiredName)
				{
				if (allProperties[j].owned < 10)
					{
					currentProperty.requiredCost = ((10 - allProperties[j].owned)/10)* allProperties[j].cost;
					}
				currentProperty.requiredId   = allProperties[j].id;
				break;
				}
			}
		if (currentProperty.income > 0 && currentProperty.cost > 0)
			{
	        	currentProperty.roi = currentProperty.income / (currentProperty.cost + currentProperty.requiredCost);
	        	}
	        if (currentProperty.roi > 0)
	        	{
			var roiTime = (1/currentProperty.roi) / payments; // days
			if (roiTime > 3652.5)
				{ // display years
				roiTime /= 365.25;
				var roiTimeText = ' years)';
				}
			else if (roiTime > 365.25)
				{ // display months
				roiTime /= 30.4375;
				var roiTimeText = ' months)';
				}
			else
				{
	        		var roiTimeText = ' days)';
	        		}
	        	}
		currentProperty.roi = Math.round(currentProperty.roi * 1000000)/1000000;
	        var str = 'ROI: ' + currentProperty.roi + ' (' + (Math.round(roiTime * 100)/100) + roiTimeText;
		var roiText = document.createElement("div");
		roiText.innerHTML=str;
		property2Rows.snapshotItem(currentRow).parentNode.parentNode.parentNode.insertBefore(roiText,property2Rows.snapshotItem(currentRow).parentNode.parentNode.parentNode.nextSibling);
		str = currentRowHtml.substring(currentRowHtml.indexOf('<strong>')+8);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.name = str;
		str = currentRowHtml.substring(currentRowHtml.indexOf('>Owned:')+8);
		str = str.substring(0,str.indexOf('<'));
		currentProperty.owned = parseInt(str);
		currentProperty.id = currentRow+length+1;
		if(bestProperty.roi < currentProperty.roi)
			{
			bestProperty = currentProperty;
			nextProperty = currentProperty.cost;
			GM_setValue('nextProperty',nextProperty);
			}
		allProperties.push(currentProperty);
		}
		
		index = bestProperty.requiredId;
		if (index >0)
			{
			if (allProperties[index].owned < 10) 
				{
				bestProperty = allProperties[index];
				}
			}
		//alert (bestProperty.id +'\n'+ bestProperty.name +'\n'+bestProperty.cost +'\n'+bestProperty.mobsize+'\n');
	}